package cn.topevery.garden.dal.logic;

import cn.topevery.garden.dal.dao.FacilitiesDao;
import cn.topevery.garden.dal.dao.ParkingLotDao;
import cn.topevery.garden.dal.util.GeometryExtHelper;
import cn.topevery.garden.entity.dto.ParkingLotDTO;
import cn.topevery.garden.entity.po.Facilities;
import cn.topevery.garden.entity.po.ParkingLot;
import cn.topevery.garden.entity.query.ParkingLotQuery;
import cn.topevery.garden.framework.base.BaseLogic;
import hw.topevery.framework.GeometryHelper;
import hw.topevery.framework.db.base.BaseEntityDao;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.enums.GeometryTypeEnum;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 *
 * @author code-generate
 * @date 2021-08-06
 */
@Service
public class ParkingLotLogic extends BaseLogic<ParkingLot> {
    @Autowired
    ParkingLotDao parkingLotDao;

    @Autowired
    private FacilitiesDao facilitiesDao;

    @Override
    public BaseEntityDao<ParkingLot, Integer> getDao() {
        return parkingLotDao;
    }

    

    /**
     * 获取列表
     *
     * @param query
     */
    public DataPager<ParkingLot> listPage(ParkingLotQuery query) {
        return parkingLotDao.listPage(query);
    }

    /**
     * 根据自增id批量删除
     *
     * @param ids
     */
    public void deleteBatchById(String userId,Integer[] ids) {
        parkingLotDao.delete(userId, ids);
    }

    public void saveOrUpdate(String userId, ParkingLotDTO dto) {
        ParkingLot parkingLot = new ParkingLot();
        BeanUtils.copyProperties(dto, parkingLot);
        Facilities facilities = new Facilities();
        facilities.setId(dto.getFacilitiesId());
        facilities.setName(dto.getName());
        facilities.setNameShort(dto.getNameShort());
        facilities.setParkId(dto.getParkId());
        facilities.setParkName(dto.getParkName());
        facilities.setExtentId(dto.getExtentId());
        facilities.setExtentName(dto.getExtentName());
        facilities.setTypeId("847e0063-ade7-4486-8539-a275d71e756b");
        facilities.setTypeName("停车场");
        facilities.setAddress(dto.getAddress());
        facilities.setStatus((short) 1);
        facilities.setGeomType(2);
        facilities.setGeom(GeometryHelper.getGeometry(GeometryExtHelper.ListtoWellKnownText(dto.getMapRangesPoints(), GeometryTypeEnum.PLANE)));
        facilitiesDao.saveOrUpdate(userId, facilities);
        parkingLot.setFacilitiesId(facilities.getId());
        parkingLotDao.saveOrUpdate(userId, parkingLot);
    }
}
